package com.vayne.injectdemo;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author xuerui.zhang
 * @since 2020/4/13 下午9:22
 */
public class APISetterInjectionDemo {

    public static void main(String[] args) {
        // 创建应用上下文：BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // 注册 CourseHolder 的 BeanDefinition
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(CourseHolder.class);
        beanDefinitionBuilder.addPropertyReference("course", "category");
        applicationContext.registerBeanDefinition("courseHolder", beanDefinitionBuilder.getBeanDefinition());

        // 加载 Course Bean 资源
        XmlBeanDefinitionReader beanDefinition = new XmlBeanDefinitionReader(applicationContext);
        String path = "classpath:/dependency_lookup_config.xml";
        beanDefinition.loadBeanDefinitions(path);

        // 启动应用上下文
        applicationContext.refresh();

        CourseHolder courseHolder = applicationContext.getBean(CourseHolder.class);
        System.out.println(courseHolder);

        applicationContext.close();
    }
}
